<?

/**
 * DevSuite
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://suite.rapidcodefactory.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@rapidcodefactory.com so we can send you a copy immediately.
 *
 * @category   RFC_Model
 * @package    RCF_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
 * @version    
*/



/**
 * Class for DB_Object Model
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
require_once('Statuses.php');
require_once('Users.php');
require_once('Categories.php');
class UsersRequests extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=int, maxLength=, allowNulls=no
	protected $usersId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $statusesId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $categoriesId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=yes
	protected $request;	//primaryKey=no, dataType=varchar, maxLength=500, allowNulls=yes
	protected $addDate;	//primaryKey=no, dataType=datetime, maxLength=, allowNulls=yes
	
	protected $status;
	protected $user;
	protected $category;
	
	public function getStatus(){
		if (is_null($this->status)){
			$this->status = ModelMapper::instance('Statuses')->find($this->statusesId);
		}
		
		return $this->status;
	}
	
	public function getUser(){
		if (is_null($this->user)){
			$this->user = ModelMapper::instance('Users')->find($this->usersId);
		}
		
		return $this->user;
	}
	
	public function getCategory(){
		if (is_null($this->category)){
			$this->category = ModelMapper::instance('Categories')->find($this->categoriesId);
		}
		
		return $this->category;
	}
}




/**
 * Class for DB_Object ModelMapper
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class UsersRequestsMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'users_requests';
		$this->selectViewName = 'v_users_requests';
		$this->selectFields = '
				id,
				users_id,
				statuses_id,
				categories_id,
				request,
				add_date,
				st_id,
			    st_statuses_catalogues_id,
			    st_status,
			    st_description,
			    sc_id,
			    sc_catalogue,
			    ct_id,
			    ct_category,
			    ct_weight,
			    ct_level,
			    ct_parent_id,
			    u_id,
			    u_apps_id,
			    u_statuses_id,
			    u_username,
			    u_password,
			    u_first_name,
			    u_last_name,
			    u_email,
			    u_registration_date,
			    u_last_update_date,
			    u_is_administrator,
			    u_is_operator,
			    u_system_roles_id,
			    u_main_image_id
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_users_requests (@p_id,
				?,
				?,
				?,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_users_requests (
				?,
				?,
				?,
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_users_requests (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		
		
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setUsersId($array['users_id']);
		$obj->setStatusesId($array['statuses_id']);
		$obj->setCategoriesId($array['categories_id']);
		$obj->setRequest($array['request']);
		$obj->setAddDate($array['add_date']);

		$arr = ArrayUtils::splitByPrefix($array, array('st_', 'u_', 'ct_', 'sc_'));
		
		
		if (!empty($arr['sc_']))
			ModelMapper::instance('StatusesCatalogues')->loadArray($arr['sc_']);
		if (!empty($arr['st_']))
			ModelMapper::instance('Statuses')->loadArray($arr['st_']);
		if (!empty($arr['ct_']))
			ModelMapper::instance('Categories')->loadArray($arr['ct_']);
		if (!empty($arr['u_']))
			ModelMapper::instance('Users')->loadArray($arr['u_']);
			
		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getUsersId(),
			$obj->getStatusesId(),
			$obj->getCategoriesId(),
			$obj->getRequest(),
			$obj->getAddDate()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getUsersId(),
			$obj->getStatusesId(),
			$obj->getCategoriesId(),
			$obj->getRequest(),
			$obj->getAddDate()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE users_requests');
		return $row['Auto_increment'];

	}
}


/**
 * Class for DB_Object Model
 *
 * @category   RFC_ModelCollection
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class UsersRequestsCollection extends ModelCollection {
	function add(UsersRequests $model){
		$this->doAdd($model);
	}
}


/**
 * Class for DB_Object Model
 *
 * @category   RFC_ModelCollectionExtended
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class UsersRequestsCollectionExtended extends UsersRequestsCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>
